#ifndef _SOCKET_H_
#define _SOCKET_H_

#include "Include.h"
#include "Packet.h"


class SocketWorker : public ACE_Task<ACE_MT_SYNCH> 
{
private:
		std::string remote_addr;
		ACE_Reactor *m_pReactor;
		ACE_Thread_Mutex m_SendLock;	
public:

	_SOCKET_STATE state;
	ACE_SOCK_Stream  m_Stream;
	int m_nCheckCount;
	long m_nTimerID;

	time_t m_SendTime;

	BYTE		m_SaveBuffer[MAX_BUFFER];
	int			m_nSaveSize;

	bool	timeout;

public:

	SocketWorker()
	{
		remote_addr = "";
		m_bStop = false;

		m_nSaveSize = 0;
		m_nCheckCount = 0;


		m_pReactor = NULL;
		m_nTimerID = -1;

		m_SendTime = time(NULL);

		state = _RUNNING;		

		timeout = false;
	}

	void SetStream(ACE_Reactor * reactor , const std::string& addr ,  ACE_SOCK_Stream & stream)
	{
		m_pReactor = reactor;
		remote_addr = addr;
		m_Stream = stream;
		this->activate();

		ACE_Time_Value timer_delay(Settings::GetSettings()->CONNECTION_CHECK_DELAY , 0);
		m_nTimerID = m_pReactor->schedule_timer (this, 0, ACE_Time_Value::zero, timer_delay);
	}

	virtual ~SocketWorker()
	{
		if(m_nTimerID != -1 && m_pReactor != NULL)
		{
			Stop();
			m_pReactor->cancel_timer(m_nTimerID);
		}

		printf("delete socketworker\n");
		LOG::PushProcessLog(_INFO , GetRemoteAddr() + " : connection closed");
	}

	bool m_bStop;

	int Stop();
	int svc(void);
	virtual bool Execute(const std::string& rcv_text){return true;}; 


	std::string GetRemoteAddr();

	int CheckConnection();
	int Send(PACKET_BASE& packet);

    virtual ACE_HANDLE get_handle(void) const;
	virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);
	virtual int handle_close (ACE_HANDLE h, ACE_Reactor_Mask mask) = 0;
	virtual int handle_timeout (const ACE_Time_Value &current_time, const void *act = 0);

};





#endif
